package com.szh.taobaounion.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.szh.taobaounion.Presenter.impl.ISelectedPagePresenterImpl;
import com.szh.taobaounion.Presenter.impl.TicketPresenterImpl;
import com.szh.taobaounion.R;
import com.szh.taobaounion.base.BaseFragment;
import com.szh.taobaounion.model.domain.IBaseInfo;
import com.szh.taobaounion.model.domain.SelectedContent;
import com.szh.taobaounion.model.domain.SelectedPageCategory;
import com.szh.taobaounion.ui.activity.TicketActivity;
import com.szh.taobaounion.ui.adapter.SelectedPageContentAdapter;
import com.szh.taobaounion.ui.adapter.SelectedPageLeftAdapter;
import com.szh.taobaounion.utils.LogUtils;
import com.szh.taobaounion.utils.PresenterManager;
import com.szh.taobaounion.utils.SizeUtils;
import com.szh.taobaounion.utils.TicketUtil;
import com.szh.taobaounion.view.ISelectedPageCallaBack;

import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class SelectedFragment extends BaseFragment implements ISelectedPageCallaBack, SelectedPageLeftAdapter.OnLeftItemClickListener, SelectedPageContentAdapter.SelectedPageContentClickListener {

    @BindView(R.id.left_category_list)
    public RecyclerView leftCategoryList;

    @BindView(R.id.right_content_list)
    public RecyclerView rightContentList;

    @BindView(R.id.fragment_bar_title_tv)
    public TextView title;
    private ISelectedPagePresenterImpl mSelectedPagePresenter;
    private SelectedPageLeftAdapter mLeftAdapter;
    private SelectedPageContentAdapter mRightAdapter;

    @Override
    protected void initPresenter() {
        mSelectedPagePresenter = PresenterManager.getInstance().getmSelectedPagePresenter();
        mSelectedPagePresenter.registerViewCallBack(this);
        mSelectedPagePresenter.getCategories();

    }

    @Override
    protected void release() {
        super.release();
        if (mSelectedPagePresenter != null) {
            mSelectedPagePresenter.unregisterViewCallBack(this);
        }
    }

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_selected;
    }
    @Override
    protected View lodeRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_lwith_bar_ayout,container,false);
    }

    @Override
    protected void initView(View rootView) {
        setUpState(State.SUCCESS);
        title.setText("精选宝贝");
        leftCategoryList.setLayoutManager(new LinearLayoutManager(getContext()));
        rightContentList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int topAndBottom = SizeUtils.dip2px(getContext(), 4);
                int leftAndRight = SizeUtils.dip2px(getContext(), 6);
                outRect.left = leftAndRight;
                outRect.right = leftAndRight;
                outRect.top = topAndBottom;
                outRect.bottom = topAndBottom;
            }
        });
        mLeftAdapter = new SelectedPageLeftAdapter();
        leftCategoryList.setAdapter(mLeftAdapter);
        mLeftAdapter.setOnLeftItemClickListener(this);

        rightContentList.setLayoutManager(new LinearLayoutManager(getContext()));
        mRightAdapter = new SelectedPageContentAdapter();
        rightContentList.setAdapter(mRightAdapter);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mLeftAdapter.setOnLeftItemClickListener(this);
        mRightAdapter.SetOnSelectedPagerClick(this);
    }

    @Override
    public void onCategoryLoad(SelectedPageCategory categories) {
        setUpState(State.SUCCESS);
        mLeftAdapter.setData(categories);

        LogUtils.d(this, "onCategoryLoad--->" + categories);
        //List<SelectedPageCategory.DataBean> data = categories.getData();


    }

    @Override
    public void onContentLoad(SelectedContent content) {
        //LogUtils.d(this,"onContentLoad---->"+content);
        mRightAdapter.setData(content);
        rightContentList.scrollToPosition(0);
    }

    @Override
    public void retry() {
        if (mSelectedPagePresenter != null) {
            mSelectedPagePresenter.reLoadContent();
        }
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading()  {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {

    }


    @Override
    public void onLeftItemClick(SelectedPageCategory.DataBean item) {
        //左边的分类点击了
        mSelectedPagePresenter.getContentByCategoryId(item);
        int favorites_id = item.getFavorites_id();
        LogUtils.d(this, "current selected item -- > " + item.getFavorites_title());
        LogUtils.d(this, "favorites_id > " + favorites_id);
    }

    @Override
    public void OnSelectedPagerClick(IBaseInfo item) {
        TicketUtil.toTicketPage(getContext(),item);
    }
}
